<?php
/**
 * Date: 2018/6/27
 * Time: 18:02
 * Author: zeronlee
 */

namespace App\Http\Controllers\Backend\ProjectManage;

use App\Http\Controllers\Controller;
use App\Models\ProjectManage\CheckOption;
use Illuminate\Http\Request;
use App\Models\Flaw\Vuls;
use Illuminate\Support\Facades\DB;

class CheckOptionController extends Controller
{

    /**
     * 列表
     */
    public function index (Request $request) {
        #learn route
        $pageNum = 10;
        $appId = DB::table('project_task_app')->where(['project_task_id'=>intval($request->tid)])->value('app_id');
        return view('backend.projectmanage.ckindex')->with('tid', intval($request->tid))->with('pageNum',$pageNum)->with('appId',$appId);
    }

    /**
     * 新建页面
     */
    public function addPage (Request $request) {

        return view('backend.projectmanage.ckadd');
    }

    /**
     * 新建
     */
    public function addRow (Request $request) {
        $ck = new CheckOption();
        $validator = $ck->validate($request);
        if ($validator->fails()) {
            return redirect('admin/ckaddpage')->withErrors($validator)->withInput();
        }
        try {

            $ck->addRow($request);
        } catch (\Exception $e) {
            \Log::info($e->getMessage());
        }
        return redirect('/admin/ckindex');
    }

    /**
     * 查询
     */
    public function search (Request $request) {
        $ck = new CheckOption();
        $filter = $request->search['value'];
        if (empty($filter)) {
            $filter = '';
        }
        $ck->initWhere($filter);
        $sel = ['check_option.id', 'check_option.name', 'check_option.created_at', 'check_option.updated_at','check_option.opt_type'];
        $list = $ck->lists($sel, [$request->start, $request->length], $request->tid);
        //总数
        $count = $ck->getCount();
        $data = ['draw' => $request->draw, 'recordsTotal' => $count, 'recordsFiltered' => $count, 'data' => $list];
        return response()->json($data);
    }

    /**
     * 更新
     * @param Request $request
     */
    public function update (Request $request) {
    }

    /**
     * 漏洞列表
     */
    public function vuls (Request $request) {
        $tId = intval($request->tid);
        $ckId = intval($request->ckid);
        $his = intval($request->his);
        $where = ['vuls.check_id' => intval($ckId), 'project_task_app.project_task_id' => intval($tId)];

        if ($his == 1) {
            $list = CheckOption::vulListOfCkHis($ckId, $tId);
        } else {

            $list = CheckOption::vulListOfCk($ckId, $tId);
        }
        return view('backend.projectmanage.vulslist', [
            'list' => $list,
            'vulStatus' => Vuls::$vulsStatus,
            'his' => $his,
            'tid' => $tId,
            'ckid'=>$ckId
        ]);
    }

}